package com.mriassl.stream.web;

import java.io.IOException;
import java.text.MessageFormat;

import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mriassl.stream.lib.rabbitmq.RabbitMQConst;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

@RestController
public class LoginController {

    @Autowired
    private ConnectionFactory connectionFactory;
    
	@RequestMapping("/login/{uid}")
	public String login(@PathVariable("uid")String uid) throws IOException{
	    Connection conn = connectionFactory.createConnection();
	    Channel channel = conn.createChannel(false);
	    channel.exchangeDeclare(RabbitMQConst.EXCHANGE_FOR_TICKET, BuiltinExchangeType.DIRECT, true);
	    String queueName = MessageFormat.format(RabbitMQConst.QUEUE_FOR_SHOPKEEPER, uid);
	    channel.queueDeclare(queueName, true, false, false, null);
	    channel.queueBind(queueName, RabbitMQConst.EXCHANGE_FOR_TICKET, MessageFormat.format(RabbitMQConst.ROUTE_FOR_SHOPKEEPER_SELF, uid));
		
	    return "保存成功";
	}

	@RequestMapping("/logout/{uid}")
	public String logout(@PathVariable("uid")String uid){
	    
	    return "发送成功:"+System.currentTimeMillis();
	}

}
